package top.wherewego.vnt.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.kongzue.baseframework.BaseBindingFragment;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;

import java.util.ArrayList;

import top.wherewego.vnt.APP;
import top.wherewego.vnt.R;
import top.wherewego.vnt.activity.MainActivity;
import top.wherewego.vnt.bean.Conf;
import top.wherewego.vnt.databinding.FragConfigBinding;
import top.wherewego.vnt.view.ConfigListAdapter;
import top.wherewego.vnt.view.DefaultAdapter;

public class ConfigListFragment extends BaseBindingFragment<MainActivity, FragConfigBinding> {


    private ConfigListAdapter adapter;

    @Override
    public void initDatas() {

    }

    @Override
    public void setEvents() {
        adapter = new ConfigListAdapter(new ArrayList<>());
        binding.rv.setLayoutManager(new LinearLayoutManager(me));
        binding.rv.setAdapter(adapter);
        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<Conf>() {
            @Override
            public void onItemClick(@NonNull View view, int viewType, @NonNull Conf data, int position) {
                switch (view.getId()) {
                    case R.id.delete:
                        MessageDialog.show("删除", "确定删除" + data.getConName() + "?", "确定", "取消")
                                .setOkButtonClickListener(new OnDialogButtonClickListener<MessageDialog>() {
                                    @Override
                                    public boolean onClick(MessageDialog dialog, View v) {
                                        APP.deleteConf(position);
                                        adapter.setData(APP.getConfList());
                                        return false;
                                    }
                                }).setCancelButtonClickListener(new OnDialogButtonClickListener<MessageDialog>() {
                                    @Override
                                    public boolean onClick(MessageDialog dialog, View v) {
                                        dialog.dismiss();
                                        return false;
                                    }
                                });
                        break;
                }
            }
        });
        adapter.setData(APP.getConfList());
    }

    @Override
    public void onShow(boolean isSwitchFragment) {
        super.onShow(isSwitchFragment);
        adapter.setData(APP.getConfList());
    }
}
